package com.tgy.blog.controller;

import com.tgy.ao.CommentAo;
import com.tgy.blog.service.DeskArticleService;
import com.tgy.page.PageQuery;
import com.tgy.page.TableDataInfo;
import com.tgy.pojo.BlogArticle;
import com.tgy.result.GraceJSONResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;

/**
 * @author tianguiyin
 * @version 1.0
 * @date 2022/7/9 18:59
 */
@Validated
@Api(value = "博客文章控制器", tags = {"博客文章"})
@RestController
@RequestMapping("/blog/article")
public class DeskArticleController {

    @Resource
    private DeskArticleService deskArticleService;


    @ApiOperation("首页获取文章列表")
    @GetMapping("getIndexArticleList")
    public GraceJSONResult getIndexArticleList(PageQuery pageQuery){
        return GraceJSONResult.ok(deskArticleService.getIndexArticleList(pageQuery));
    }

    @ApiOperation("获取排行榜")
    @GetMapping("getRankingArticleList")
    public GraceJSONResult getRankingArticleList(PageQuery pageQuery){
        return GraceJSONResult.ok(deskArticleService.getRankingArticleList(pageQuery));
    }

    @ApiOperation("站长推荐文章")
    @GetMapping("getRecommendedArticleList")
    public GraceJSONResult getRecommendedArticleList(PageQuery pageQuery){
        return GraceJSONResult.ok(deskArticleService.getRecommendedArticleList(pageQuery));
    }

    @ApiOperation("导航分类")
    @GetMapping("getCategory")
    public GraceJSONResult getCategory(){
        return GraceJSONResult.ok(deskArticleService.getCategory());
    }

    /**
     * 根据分类查询，按浏览量和时间倒叙返回
     */
    @ApiOperation("相似文章")
    @GetMapping("getSimilarArticleList")
    public GraceJSONResult getSimilarArticleList(String categoryIds,PageQuery pageQuery){
        return GraceJSONResult.ok(deskArticleService.getSimilarArticleList(categoryIds,pageQuery));
    }

    /**
     * 首页数据，查第一页10条即可
     */
    @ApiOperation("随便看看")
    @GetMapping("getCasualArticleList")
    public GraceJSONResult getCasualArticleList(PageQuery pageQuery){
        pageQuery.setPage(1);
        TableDataInfo<BlogArticle> indexArticleList = deskArticleService.getIndexArticleList(pageQuery);
        return GraceJSONResult.ok(indexArticleList.getRows());
    }


    @ApiOperation("文章专栏获取文章列表")
    @GetMapping("getArticleArticleList")
    public GraceJSONResult getArticleArticleList(BlogArticle bo , PageQuery pageQuery){
        return GraceJSONResult.ok(deskArticleService.getArticleArticleList(bo,pageQuery));
    }

    @ApiOperation("增加文章观看数量")
    @GetMapping("addArticleWatch")
    public GraceJSONResult addArticleWatch(String articleId){
        return deskArticleService.addArticleWatch(articleId);
    }

    @ApiOperation("评论文章/留言")
    @PostMapping("addComment")
    public GraceJSONResult addComment(CommentAo ao){
        return deskArticleService.addComment(ao);
    }

    @ApiOperation("加载评论和留言")
    @PostMapping("getComment")
    public GraceJSONResult getComment(PageQuery pageQuery, String articleId){
        return GraceJSONResult.ok(deskArticleService.getComment(pageQuery,articleId));
    }

    @ApiOperation("加载热评用户")
    @GetMapping("getBuzzUser")
    public GraceJSONResult getBuzzUser(){
        return GraceJSONResult.ok(deskArticleService.getBuzzUser());
    }

    @ApiOperation("加载最近留言")
    @GetMapping("getRecentlyComment")
    public GraceJSONResult getRecentlyComment(){
        return GraceJSONResult.ok(deskArticleService.getRecentlyComment());
    }

    @ApiOperation("获取Git提交记录")
    @GetMapping("getGitCommitHistory")
    public GraceJSONResult getGitCommitHistory() throws ParseException {
        return GraceJSONResult.ok(deskArticleService.getGitCommitHistory());
    }

}
